import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "User_Servlet", urlPatterns = {"/login/dgut"})
public class User_Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session=request.getSession();
        response.getWriter().println("hello");
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        HttpClient http=new HttpClient();
        String strs=request.getParameter("token");
        PersonDTO p=new PersonDTO(strs,"javaee","b3b52e43ccfd","abx");
        Jsonb jsonb = JsonbBuilder.create();
        String st=jsonb.toJson(p);
        String s=http.postJson("https://cas.dgut.edu.cn/ssoapi/v2/checkToken",p);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println(s);
        String json=http.postJson("https://cas.dgut.edu.cn/oauth/getUserInfo",s);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println(json);
        session.setAttribute("json",json);
        response.sendRedirect("http://localhost:8080/user.jsp");
    }
}
